import { formatterDigit, isNumber } from '@vc/utils';

/**
 * 跳转来源
 */
export const toSource = (fileType: string, fileUrl: string) => {
  const pageUrl = ['html', 'zip'].indexOf(fileType) > -1 ? `${location.origin}/zlibHtml.html?url=${fileUrl}` : `${fileUrl}?title=详情&htmlTitle=false`;
  window.open(pageUrl);
};

/**
 * 动态引入图片
 */
export const getImageUrl = (url: string) => {
  return new URL(`../assets/${url}`, import.meta.url).href;
};

/**
 * 年份选择相关：将接口返回的纯数字数组转为选择器需要的格式
 */
export const transferYearToMultipleSelector = (years: Array<number>) => {
  return years.map((year) => {
    return {
      name: year + '年',
      code: year + ''
    };
  });
};

/** 评价等级显示逻辑 */
export const handleTagNegativeShow = (tagNegative: number, evaluationResult: string) => {
  if (tagNegative === 0) {
    return '一般';
  } else if (tagNegative === 1) {
    return '良好';
  } else if (tagNegative === 2) {
    return '诚信';
  } else if (tagNegative === -1 && (evaluationResult === 'C' || evaluationResult === '黄色')) {
    return '一般';
  } else if (tagNegative === -1 && !(evaluationResult === 'C' || evaluationResult === '黄色')) {
    return '警示';
  } else if (tagNegative === -2 && (evaluationResult === 'D' || evaluationResult === '红色')) {
    return '警示';
  } else if (tagNegative === -2 && !(evaluationResult === 'C' || evaluationResult === '黄色')) {
    return '不良';
  } else if (tagNegative === -3) {
    return '不良';
  }
};

/**
 * 计算表格最大高度 (30为底部状态栏的高度)
 */
export const calculateTableMaxHeight = () => {
  return document.body.scrollHeight - (document.querySelector('.page-content-table') as HTMLDivElement).getBoundingClientRect().top - (document.querySelector('.vxe-pager') as HTMLDivElement).offsetHeight - 30;
};


/**
 * @description 格式化金额的值 添加千分位
 * */
export function formatMoneyValue(value) {
  if (value === 0) return 0;
  else if (!value) return;
  else if (!isNumber(Number(value))) return;
  const val = value.replace(/,/g, '').split('.')[0];
  // 一百
  const hundred = 100;
  // 一千
  const oneThousand = 1000;
  // 一万
  const tenThousand = 10000;
  // 一千万
  const tenMillion = oneThousand * tenThousand;
  // 一亿
  const oneHundredMillion = tenThousand * tenThousand;
  // 一百亿
  const tenBillion = hundred * oneHundredMillion;

  // 大于 一千万 小于 100 亿
  if (val >= tenMillion && val <= tenBillion) {
    return formatterDigit(val / tenThousand, 2) + '万';
  }
  // 大于 100 亿
  else if (val > tenBillion) {
    return formatterDigit(val / oneHundredMillion, 2) + '亿';
  } else {
    return value;
  }
}

//时间转换
export const timeTrans = (dateTimeStamp) => {
  var minute = 1000 * 60;      //把分，时，天，周，半个月，一个月用毫秒表示
  var hour = minute * 60;
  var now = new Date().getTime();   //获取当前时间毫秒
  const nowYear = new Date().getFullYear()
  const nowMonth = new Date().getMonth() + 1
  var diffValue = now - dateTimeStamp;//时间差
  if (diffValue < 0) {
    return;
  }
  var minC = diffValue / minute;  //计算时间差的分，时，天，周，月
  var hourC: any = diffValue / hour;
  const date = new Date(dateTimeStamp);
  const stampYear = date.getFullYear();
  const stampMonth = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
  const stampDate = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  var result;
  if (minC <= 5) {
    result = "刚刚"
  } else if (minC <= 10) {
    result = "5分钟前"
  } else if (minC <= 30) {
    result = "10分钟前"
  } else if (hourC <= 1) {
    result = "半小时前"
  } else if (hourC <= 12) {
    result = `${parseInt(hourC)}小时前`
  } else if (nowYear === stampYear && nowMonth === stampMonth) {
    // 同年同月 显示月份日期
    result = `${stampMonth}-${stampDate}`
  } else {
    result = `${stampYear}-${stampMonth}-${stampDate}`
  }
  return result;
}

